package com.wctj.api.web.config;

import com.wctj.api.business.common.manager.UserManager;
import com.wctj.api.security.component.DynamicSecurityService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.userdetails.UserDetailsService;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * mall-security 模块相关配置
 * @author carol
 */
@Configuration
public class MallSecurityConfig {

    @Resource
    private UserManager userManager;

    @Bean
    public UserDetailsService userDetailsService() {
        //获取登录用户信息
        return username -> userManager.loadUserByUsername(username);
    }

    @Bean
    public DynamicSecurityService dynamicSecurityService() {
        return new DynamicSecurityService() {
            @Override
            public Map<String, ConfigAttribute> loadDataSource() {
                Map<String, ConfigAttribute> authMap = new ConcurrentHashMap<>(7);
//                //TODO 放行所有 加载所有的资源
//                List<IzsAuthDTO> listAll = izsAuthService.listAll();
//                for (IzsAuthDTO authDTO : listAll) {
//                    authMap.put(authDTO.getUrl(), new org.springframework.security.access.SecurityConfig(authDTO.getId() + ":" + authDTO.getName()));
//                }

                return authMap;
            }
        };
    }
}
